home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 1996 November & December / Amiga-CD 1996 #11-12.iso / pd-disketten / dms-gepackt / 2_96 / apd-2-96-2.dms / apd-2-96-2.adf / Profiler C++ / profile.c < prev    next >
C/C++ Source or Header  |  1996-01-16  |  2KB  |  83 lines

  1. /* Profile.c - © '95 by Clemens Marschner */
  2.  
  3. #include <pragma/timer_lib.h>
  4. #include <pragma/exec_lib.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <stream.h>
  9.  
  10. struct Library *OpenTimerLib();
  11. struct Library *TimerBase = OpenTimerLib();
  12. void CloseTimerLib(), InitSinTab();
  13.  
  14. inline double sintab(double);
  15. double sint[360];
  16. const double pi = 3.141592654;
  17. const double hdpi = 128/pi, pidh = pi/128;
  18. struct timerequest tr;
  19.  
  20. class Profiler {
  21.   timeval tv1,tv2;
  22.   char *string;
  23. public:
  24.   Profiler(char *desc) {     // Konstruktor
  25.       if(!TimerBase) throw int(1);
  26.       string = new char[strlen(desc)+1];
  27.       if(!string)    throw int(2);
  28.       strcpy(string, desc);  // und kopieren
  29.       GetSysTime(&tv1);  // Zeit in timeval1
  30.   }
  31.   ~Profiler() {
  32.       GetSysTime(&tv2);  // Zeit nach timeval2.
  33.       SubTime(&tv2, &tv1);  // erste davon abzieh.
  34.       cout << string << ": " << double(tv2.tv_secs)*
  35.        1000 +double(tv2.tv_micro) * 1e-3 << " ms\n";
  36.       delete string;
  37.   }
  38. };
  39.  
  40. void main() {
  41.     if(!TimerBase) return;
  42.     InitSinTab();
  43.     cout << "Profilertest: 1000 mal Sinus.\nEinmal "
  44.             "Systemfunktion, einmal aus Tabelle\n";
  45.     double result;
  46.     try {
  47.         {Profiler p("sin()");
  48.             for(double r = 0; r<2*pi; r+= 2*pi/1000)
  49.                 result = sin(r);
  50.         }
  51.         {Profiler p("sintab()");
  52.             for(double r = 0; r<2*pi; r+= 2*pi/1000)
  53.                 result = sintab(r);
  54.         }
  55.         {Profiler p("Profiler");
  56.             {Profiler p("leer");  }
  57.         }
  58.     } catch(...) { cout << "Fehler!\n"; }   
  59.     CloseTimerLib();
  60. }
  61.  
  62. struct Library *OpenTimerLib() {
  63.     int error = OpenDevice(TIMERNAME, UNIT_MICROHZ,
  64.                 (struct IORequest*)&tr,0);
  65.     if(error) return 0;
  66.     return (struct Library*)tr.tr_node.io_Device;
  67. }
  68.  
  69. void CloseTimerLib() {
  70.     CloseDevice((struct IORequest*)&tr);
  71. }
  72.  
  73. void InitSinTab() {
  74.     for(int i = 0; i < 360; i++)
  75.         sint[i] = sin(double(i)*pidh);
  76. }
  77.  
  78. inline double sintab(double phi) {      
  79.     return sint[int((phi*hdpi)+0.5)%360];
  80.      // int(x+0.5) rundet richtig, nicht nur ab
  81. }
  82.  
  83.